package com.bookstore.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bookstore.entity.po.BookMsgPo;
import com.bookstore.entity.po.OrderPO;
import com.bookstore.entity.pojo.R;
import com.bookstore.service.BookMsgService;
import com.bookstore.service.BookService;
import com.bookstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("bookMsg")
public class BookMsgController {
    @Autowired
    BookMsgService bookMsgService;
    @Autowired
    OrderService orderService;
    @Autowired
    BookService bookService;

    @PostMapping("add")
    public R test (@RequestBody HashMap<String ,String> params) {
        String commendMsg = params.get("commendMsg");
        String star = params.get("star");
        String userName = params.get("userName");
        String bookid = params.get("bookid");
        String orderId = params.get("orderId");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String date = simpleDateFormat.format(new Date());
        BookMsgPo bookMsgPo = new BookMsgPo(null,Integer.parseInt(bookid),userName,commendMsg.replaceAll("\n","<br/>"),date,star,null);


        OrderPO orderPO = orderService.getById(Integer.parseInt(orderId));

        orderPO.setIsCritical(OrderService.IS_CRITICAL);

        orderService.updateById(orderPO);
        return  bookMsgService.save(bookMsgPo)&&orderService.updateById(orderPO)?R.success():R.error();
    }

    @GetMapping("getByBookId")
    public R getByBookId(String  bookId){
        List<BookMsgPo> book_id = bookMsgService.list(new QueryWrapper<BookMsgPo>().eq("book_id", bookId));
        return R.success(book_id);
    }
    @GetMapping("getAllByUserName")
    public R getAllByUserName(String userName){
        List<BookMsgPo> bookMsgPoList = bookMsgService.list(new QueryWrapper<BookMsgPo>().eq("user_name", userName));
        bookMsgPoList.forEach(x -> {
            x.setMsg(x.getMsg().replaceAll("<br/>","\n"));
            x.setBookName(bookService.getById(x.getBookId()).getBookName());
        });
        return R.success(bookMsgPoList);
    }
    @GetMapping("getById")
    public R getByID(Integer id) {
        BookMsgPo byId = bookMsgService.getById(id);
        return R.success(byId);
    }

    @PostMapping("updata")
    public R u(@RequestBody HashMap<String,String> params) {
        String  star = params.get("star");
        String  commend = params.get("commend");
        String  id = params.get("id");
        BookMsgPo bookMsgPo = new BookMsgPo();
        bookMsgPo.setMsg(commend.replaceAll("\n","<br/>"));
        bookMsgPo.setId(Integer.parseInt(id));
        bookMsgPo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        bookMsgPo.setStar(star);
        bookMsgService.updateById(bookMsgPo);
        return R.success();
    }
    @DeleteMapping("del/{id}")

    public R delById(@PathVariable("id") String id) {


        return bookMsgService.removeById(Integer.parseInt(id))?R.success():R.error();
    }

}
